package com.ougy.reshop.util;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.math.BigDecimal;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
import java.util.UUID;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.swing.ImageIcon;

import org.jsoup.Jsoup;
import org.jsoup.safety.Whitelist;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.ougy.reshop.model.Accessory;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class CommUtil {
	private static final SimpleDateFormat dateFormat = new SimpleDateFormat(
			"yyyy-MM-dd");
	private static final Whitelist user_content_filter = Whitelist.relaxed();

	static {
		user_content_filter.addTags(new String[] { "embed", "object", "param",
				"span", "div", "font" });
		user_content_filter.addAttributes(":all", new String[] { "style",
				"class", "id", "name" });
		user_content_filter.addAttributes("object", new String[] { "width",
				"height", "classid", "codebase" });
		user_content_filter.addAttributes("param", new String[] { "name",
				"value" });
		user_content_filter.addAttributes("embed",
				new String[] { "src", "quality", "width", "height",
						"allowFullScreen", "allowScriptAccess", "flashvars",
						"name", "type", "pluginspage" });
	}

	 public static String first2low(String str)
	  {
	    String s = "";
	    s = str.substring(0, 1).toLowerCase() + str.substring(1);
	    return s;
	  }

	  public static String first2upper(String str) {
	    String s = "";
	    s = str.substring(0, 1).toUpperCase() + str.substring(1);
	    return s;
	  }

	  public static List<String> str2list(String s)
	    throws IOException
	  {
	    List list = new ArrayList();
	    if ((s != null) && (!s.equals(""))) {
	      StringReader fr = new StringReader(s);
	      BufferedReader br = new BufferedReader(fr);
	      String aline = "";
	      while ((aline = br.readLine()) != null) {
	        list.add(aline);
	      }
	    }
	    return list;
	  }
	
	public static Date formatDate(String s) {
		Date d = null;
		try {
			d = dateFormat.parse(s);
		} catch (Exception localException) {
		}
		return d;
	}
	
	  public static Date formatDate(String s, String format) {
		    Date d = null;
		    try {
		      SimpleDateFormat dFormat = new SimpleDateFormat(format);
		      d = dFormat.parse(s);
		    } catch (Exception localException) {
		    }
		    return d;
		  }

		  public static String formatTime(String format, Object v) {
		    if (v == null)
		      return null;
		    if (v.equals(""))
		      return "";
		    SimpleDateFormat df = new SimpleDateFormat(format);
		    return df.format(v);
		  }

		  public static String formatLongDate(Object v) {
		    if ((v == null) || (v.equals("")))
		      return "";
		    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		    return df.format(v);
		  }

		  public static String formatShortDate(Object v) {
		    if (v == null)
		      return null;
		    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		    return df.format(v);
		  }

		  public static String decode(String s) {
		    String ret = s;
		    try {
		      ret = URLDecoder.decode(s.trim(), "UTF-8");
		    } catch (Exception localException) {
		    }
		    return ret;
		  }

		  public static String encodeUrl(String s) {
		    String ret = s;
		    try {
		      ret = URLEncoder.encode(s.trim(), "UTF-8");
		    } catch (Exception localException) {
		    }
		    return ret;
		  }

		  public static String convert(String str, String coding) {
		    String newStr = "";
		    if (str != null)
		      try {
		        newStr = new String(str.getBytes("ISO-8859-1"), coding);
		      } catch (Exception e) {
		        return newStr;
		      }
		    return newStr;
		  }

		  public static Map saveFileToServer(HttpServletRequest request, String filePath, String saveFilePathName, String saveFileName, String[] extendes)
		    throws IOException
		  {
		    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
		    CommonsMultipartFile file = (CommonsMultipartFile)multipartRequest
		      .getFile(filePath);
		    Map map = new HashMap();
		    if ((file != null) && (!file.isEmpty()))
		    {
		      String extend = file.getOriginalFilename().substring(
		        file.getOriginalFilename().lastIndexOf(".") + 1)
		        .toLowerCase();
		      if ((saveFileName == null) || (saveFileName.trim().equals(""))) {
		        saveFileName = UUID.randomUUID().toString() + "." + extend;
		      }
		      if (saveFileName.lastIndexOf(".") < 0) {
		        saveFileName = saveFileName + "." + extend;
		      }
		      float fileSize = Float.valueOf((float)file.getSize()).floatValue();
		      List errors = new ArrayList();
		      boolean flag = true;
		      if (extendes != null) {
		        for (String s : extendes) {
		          if (extend.toLowerCase().equals(s))
		            flag = true;
		        }
		      }
		      if (flag) {
		        File path = new File(saveFilePathName);
		        if (!path.exists()) {
		          path.mkdir();
		        }
		        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		        DataOutputStream out = new DataOutputStream(
		          new FileOutputStream(saveFilePathName + File.separator + 
		          saveFileName));
		        InputStream is = null;
		        try {
		          is = file.getInputStream();
		          int size = (int)fileSize;
		          byte[] buffer = new byte[size];
		          while (is.read(buffer) > 0)
		            out.write(buffer);
		        }
		        catch (IOException exception) {
		          exception.printStackTrace();
		        } finally {
		          if (is != null) {
		            is.close();
		          }
		          if (out != null) {
		            out.close();
		          }
		        }
		        if (isImg(extend)) {
		          File img = new File(saveFilePathName + File.separator + 
		            saveFileName);
		          try {
		            BufferedImage bis = ImageIO.read(img);
		            int w = bis.getWidth();
		            int h = bis.getHeight();
		            map.put("width", Integer.valueOf(w));
		            map.put("height", Integer.valueOf(h));
		          }
		          catch (Exception localException)
		          {
		          }
		        }
		        map.put("mime", extend);
		        map.put("fileName", saveFileName);
		        map.put("fileSize", Float.valueOf(fileSize));
		        map.put("error", errors);
		        map.put("oldName", file.getFileItem().getFieldName());
		      }
		      else
		      {
		        errors.add("不允许的扩展名");
		      }
		    } else {
		      map.put("width", Integer.valueOf(0));
		      map.put("height", Integer.valueOf(0));
		      map.put("mime", "");
		      map.put("fileName", "");
		      map.put("fileSize", Float.valueOf(0.0F));
		      map.put("oldName", "");
		    }
		    return map;
		  }

		  public static boolean isImg(String extend) {
		    boolean ret = false;
		    List<String> list = new ArrayList<String>();
		    list.add("jpg");
		    list.add("jpeg");
		    list.add("bmp");
		    list.add("gif");
		    list.add("png");
		    list.add("tif");
		    for (String s : list) {
		      if (s.equals(extend))
		        ret = true;
		    }
		    return ret;
		  }

		  public static final void waterMarkWithImage(String pressImg, String targetImg, int pos, float alpha)
		  {
		    try
		    {
		      File _file = new File(targetImg);
		      Image src = ImageIO.read(_file);
		      int width = src.getWidth(null);
		      int height = src.getHeight(null);
		      BufferedImage image = new BufferedImage(width, height, 
		        1);
		      Graphics2D g = image.createGraphics();
		      g.drawImage(src, 0, 0, width, height, null);

		      File _filebiao = new File(pressImg);
		      Image src_biao = ImageIO.read(_filebiao);
		      g.setComposite(
		        AlphaComposite.getInstance(10, 
		        alpha / 100.0F));
		      int width_biao = src_biao.getWidth(null);
		      int height_biao = src_biao.getHeight(null);
		      int x = 0;
		      int y = 0;

		      if (pos == 2) {
		        x = (width - width_biao) / 2;
		        y = 0;
		      }
		      if (pos == 3) {
		        x = width - width_biao;
		        y = 0;
		      }
		      if (pos == 4) {
		        x = width - width_biao;
		        y = (height - height_biao) / 2;
		      }
		      if (pos == 5) {
		        x = width - width_biao;
		        y = height - height_biao;
		      }
		      if (pos == 6) {
		        x = (width - width_biao) / 2;
		        y = height - height_biao;
		      }
		      if (pos == 7) {
		        x = 0;
		        y = height - height_biao;
		      }
		      if (pos == 8) {
		        x = 0;
		        y = (height - height_biao) / 2;
		      }
		      if (pos == 9) {
		        x = (width - width_biao) / 2;
		        y = (height - height_biao) / 2;
		      }
		      g.drawImage(src_biao, x, y, width_biao, height_biao, null);

		      g.dispose();
		      FileOutputStream out = new FileOutputStream(targetImg);
		      JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
		      encoder.encode(image);
		      out.close();
		    } catch (Exception e) {
		      e.printStackTrace();
		    }
		  }

		  public static boolean createSmall(String source, String target, int width) {
		    try {
		      File sourceFile = new File(source);
		      File targetFile = new File(target);
		      BufferedImage bis = ImageIO.read(sourceFile);
		      int w = bis.getWidth();
		      int h = bis.getHeight();
		      int nw = width;
		      int nh = nw * h / w;
		      ImageScale is = new ImageScale();
		      is.saveImageAsJpg(source, target, nw, nh);
		      return true;
		    } catch (Exception e) {
		      e.printStackTrace();
		    }return false;
		  }

		  public static boolean waterMarkWithText(String filePath, String outPath, String text, String markContentColor, Font font, int pos, float qualNum)
		  {
		    ImageIcon imgIcon = new ImageIcon(filePath);
		    Image theImg = imgIcon.getImage();
		    int width = theImg.getWidth(null);
		    int height = theImg.getHeight(null);
		    BufferedImage bimage = new BufferedImage(width, height, 
		      1);
		    Graphics2D g = bimage.createGraphics();
		    if (font == null) {
		      font = new Font("黑体", 1, 30);
		      g.setFont(font);
		    } else {
		      g.setFont(font);
		    }
		    g.setColor(getColor(markContentColor));
		    g.setBackground(Color.white);
		    g.drawImage(theImg, 0, 0, null);
		    FontMetrics metrics = new FontMetrics(font)
		    {
		    };
		    Rectangle2D bounds = metrics.getStringBounds(text, null);
		    int widthInPixels = (int)bounds.getWidth();
		    int heightInPixels = (int)bounds.getHeight();
		    int left = 0;
		    int top = heightInPixels;

		    if (pos == 2) {
		      left = width / 2;
		      top = heightInPixels;
		    }
		    if (pos == 3) {
		      left = width - widthInPixels;
		      top = heightInPixels;
		    }
		    if (pos == 4) {
		      left = width - widthInPixels;
		      top = height / 2;
		    }
		    if (pos == 5) {
		      left = width - widthInPixels;
		      top = height - heightInPixels;
		    }
		    if (pos == 6) {
		      left = width / 2;
		      top = height - heightInPixels;
		    }
		    if (pos == 7) {
		      left = 0;
		      top = height - heightInPixels;
		    }
		    if (pos == 8) {
		      left = 0;
		      top = height / 2;
		    }
		    if (pos == 9) {
		      left = width / 2;
		      top = height / 2;
		    }
		    g.drawString(text, left, top);
		    g.dispose();
		    try {
		      FileOutputStream out = new FileOutputStream(outPath);
		      JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
		      JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
		      param.setQuality(qualNum, true);
		      encoder.encode(bimage, param);
		      out.close();
		    } catch (Exception e) {
		      return false;
		    }
		    return true;
		  }

		  public static boolean createFolder(String folderPath) {
		    boolean ret = true;
		    try {
		      File myFilePath = new File(folderPath);
		      if ((!myFilePath.exists()) && (!myFilePath.isDirectory())) {
		        ret = myFilePath.mkdirs();
		        if (!ret)
		          System.out.println("创建文件夹出错");
		      }
		    }
		    catch (Exception e) {
		      System.out.println("创建文件夹出错");
		      ret = false;
		    }
		    return ret;
		  }

		  public static List toRowChildList(List list, int perNum)
		  {
		    List l = new ArrayList();
		    if (list == null) {
		      return l;
		    }

		    for (int i = 0; i < list.size(); i += perNum) {
		      List cList = new ArrayList();
		      for (int j = 0; j < perNum; j++)
		        if (i + j < list.size())
		          cList.add(list.get(i + j));
		      l.add(cList);
		    }
		    return l;
		  }

		  public static List copyList(List list, int begin, int end) {
		    List l = new ArrayList();
		    if (list == null)
		      return l;
		    if (end > list.size())
		      end = list.size();
		    for (int i = begin; i < end; i++) {
		      l.add(list.get(i));
		    }
		    return l;
		  }

		  public static boolean isNotNull(Object obj) {
		    if ((obj != null) && (!obj.toString().equals(""))) {
		      return true;
		    }
		    return false;
		  }

		  public static void copyFile(String oldPath, String newPath)
		  {
		    try
		    {
		      int bytesum = 0;
		      int byteread = 0;
		      File oldfile = new File(oldPath);
		      if (oldfile.exists()) {
		        InputStream inStream = new FileInputStream(oldPath);
		        FileOutputStream fs = new FileOutputStream(newPath);
		        byte[] buffer = new byte[1444];

		        while ((byteread = inStream.read(buffer)) != -1) {
		          bytesum += byteread;
		          fs.write(buffer, 0, byteread);
		        }
		        inStream.close();
		      }
		    } catch (Exception e) {
		      System.out.println("复制单个文件操作出错 ");
		      e.printStackTrace();
		    }
		  }

		  public static boolean deleteFolder(String path)
		  {
		    boolean flag = false;
		    File file = new File(path);

		    if (!file.exists()) {
		      return flag;
		    }

		    if (file.isFile()) {
		      return deleteFile(path);
		    }
		    return deleteDirectory(path);
		  }

		  public static boolean deleteFile(String path)
		  {
		    boolean flag = false;
		    File file = new File(path);

		    if ((file.isFile()) && (file.exists())) {
		      file.delete();
		      flag = true;
		    }
		    return flag;
		  }

		  public static boolean deleteDirectory(String path)
		  {
		    if (!path.endsWith(File.separator)) {
		      path = path + File.separator;
		    }
		    File dirFile = new File(path);

		    if ((!dirFile.exists()) || (!dirFile.isDirectory())) {
		      return false;
		    }
		    boolean flag = true;

		    File[] files = dirFile.listFiles();
		    for (int i = 0; i < files.length; i++)
		    {
		      if (files[i].isFile()) {
		        flag = deleteFile(files[i].getAbsolutePath());
		        if (!flag)
		          break;
		      }
		      else {
		        flag = deleteDirectory(files[i].getAbsolutePath());
		        if (!flag)
		          break;
		      }
		    }
		    if (!flag) {
		      return false;
		    }
		    if (dirFile.delete()) {
		      return true;
		    }
		    return false;
		  }

		  public static String showPageStaticHtml(String url, int currentPage, int pages)
		  {
		    String s = "";
		    if (pages > 0) {
		      if (currentPage >= 1) {
		        s = s + "<a href='" + url + "_1.htm'>首页</a> ";
		        if (currentPage > 1) {
		          s = s + "<a href='" + url + "_" + (currentPage - 1) + 
		            ".htm'>上一页</a> ";
		        }
		      }
		      int beginPage = currentPage - 3 < 1 ? 1 : currentPage - 3;
		      if (beginPage <= pages) {
		        s = s + "第　";
		        int i = beginPage; for (int j = 0; (i <= pages) && (j < 6); j++) {
		          if (i == currentPage)
		            s = s + "<a class='this' href='" + url + "_" + i + 
		              ".htm'>" + i + "</a> ";
		          else
		            s = s + "<a href='" + url + "_" + i + ".htm'>" + i + 
		              "</a> ";
		          i++;
		        }

		        s = s + "页　";
		      }
		      if (currentPage <= pages) {
		        if (currentPage < pages) {
		          s = s + "<a href='" + url + "_" + (currentPage + 1) + 
		            ".htm'>下一页</a> ";
		        }
		        s = s + "<a href='" + url + "_" + pages + ".htm'>末页</a> ";
		      }
		    }
		    return s;
		  }

		  public static String showPageHtml(String url, String params, int currentPage, int pages)
		  {
		    String s = "";
		    if (pages > 0) {
		      if (currentPage >= 1) {
		        s = s + "<a href='" + url + "?currentPage=1" + params + 
		          "'>首页</a> ";
		        if (currentPage > 1) {
		          s = s + "<a href='" + url + "?currentPage=" + (
		            currentPage - 1) + params + "'>上一页</a> ";
		        }
		      }
		      int beginPage = currentPage - 3 < 1 ? 1 : currentPage - 3;
		      if (beginPage <= pages) {
		        s = s + "第　";
		        int i = beginPage; for (int j = 0; (i <= pages) && (j < 6); j++) {
		          if (i == currentPage)
		            s = s + "<a class='this' href='" + url + "?currentPage=" + 
		              i + params + "'>" + i + "</a> ";
		          else
		            s = s + "<a href='" + url + "?currentPage=" + i + params + 
		              "'>" + i + "</a> ";
		          i++;
		        }

		        s = s + "页　";
		      }
		      if (currentPage <= pages) {
		        if (currentPage < pages) {
		          s = s + "<a href='" + url + "?currentPage=" + (
		            currentPage + 1) + params + "'>下一页</a> ";
		        }
		        s = s + "<a href='" + url + "?currentPage=" + pages + params + 
		          "'>末页</a> ";
		      }
		    }

		    return s;
		  }

		  public static String showPageFormHtml(int currentPage, int pages)
		  {
		    String s = "";
		    if (pages > 0) {
		      if (currentPage >= 1) {
		        s = s + "<a href='javascript:void(0);' onclick='return gotoPage(1)'>首页</a> ";
		        if (currentPage > 1) {
		          s = s + "<a href='javascript:void(0);' onclick='return gotoPage(" + (
		            currentPage - 1) + ")'>上一页</a> ";
		        }
		      }
		      int beginPage = currentPage - 3 < 1 ? 1 : currentPage - 3;
		      if (beginPage <= pages) {
		        s = s + "第　";
		        int i = beginPage; for (int j = 0; (i <= pages) && (j < 6); j++) {
		          if (i == currentPage)
		            s = s + "<a class='this' href='javascript:void(0);' onclick='return gotoPage(" + 
		              i + ")'>" + i + "</a> ";
		          else
		            s = s + "<a href='javascript:void(0);' onclick='return gotoPage(" + 
		              i + 
		              ")'>" + i + "</a> ";
		          i++;
		        }

		        s = s + "页　";
		      }
		      if (currentPage <= pages) {
		        if (currentPage < pages) {
		          s = s + "<a href='javascript:void(0);' onclick='return gotoPage(" + (
		            currentPage + 1) + ")'>下一页</a> ";
		        }
		        s = s + "<a href='javascript:void(0);' onclick='return gotoPage(" + 
		          pages + ")'>末页</a> ";
		      }
		    }

		    return s;
		  }

		  public static String showPageAjaxHtml(String url, String params, int currentPage, int pages)
		  {
		    String s = "";
		    String address = url + "?1=1" + params;
		    if (currentPage > 1) {
		      s = s + "<a href='javascript:void(0);' onclick='return ajaxPage(\"" + 
		        address + "\",1,this)'>首页</a> ";
		      s = s + "<a href='javascript:void(0);' onclick='return ajaxPage(\"" + 
		        address + "\"," + (currentPage - 1) + ",this)'>上一页</a> ";
		    }

		    int beginPage = currentPage - 3 < 1 ? 1 : currentPage - 3;
		    if (beginPage < pages) {
		      s = s + "第　";
		      int i = beginPage; for (int j = 0; (i <= pages) && (j < 6); j++) {
		        if (i == currentPage)
		          s = s + "<a class='this' href='javascript:void(0);' onclick='return ajaxPage(\"" + 
		            address + "\"," + i + ",this)'>" + i + "</a> ";
		        else
		          s = s + "<a href='javascript:void(0);' onclick='return ajaxPage(\"" + 
		            address + "\"," + i + 
		            ",this)'>" + i + "</a> ";
		        i++;
		      }

		      s = s + "页　";
		    }
		    if (currentPage < pages) {
		      s = s + "<a href='javascript:void(0);' onclick='return ajaxPage(\"" + 
		        address + "\"," + (currentPage + 1) + ",this)'>下一页</a> ";
		      s = s + "<a href='javascript:void(0);' onclick='return ajaxPage(\"" + 
		        address + "\"," + pages + ",this)'>末页</a> ";
		    }

		    return s;
		  }

		

		  public static char randomChar() {
		    char[] chars = { 'a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 
		      'e', 'E', 'f', 'F', 'g', 'G', 'h', 'H', 'i', 'I', 'j', 'J', 
		      'k', 'K', 'l', 'L', 'm', 'M', 'n', 'N', 'o', 'O', 'p', 'P', 
		      'q', 'Q', 'r', 'R', 's', 'S', 't', 'T', 'u', 'U', 'v', 'V', 
		      'w', 'W', 'x', 'X', 'y', 'Y', 'z', 'Z' };
		    int index = (int)(Math.random() * 52.0D) - 1;
		    if (index < 0) {
		      index = 0;
		    }
		    return chars[index];
		  }

		  public static String[] splitByChar(String s, String c) {
		    String[] list = s.split(c);
		    return list;
		  }

		  public static Object requestByParam(HttpServletRequest request, String param) {
		    if (!request.getParameter(param).equals("")) {
		      return request.getParameter(param);
		    }
		    return null;
		  }

		  public static String substring(String s, int maxLength)
		  {
		    if (!StringUtils.hasLength(s))
		      return s;
		    if (s.length() <= maxLength) {
		      return s;
		    }
		    return s.substring(0, maxLength) + "...";
		  }

		  public static String substringfrom(String s, String from) {
		    if (s.indexOf(from) < 0)
		      return "";
		    return s.substring(s.indexOf(from) + from.length());
		  }

		  public static int null2Int(Object s) {
		    int v = 0;
		    if (s != null)
		      try {
		        v = Integer.parseInt(s.toString());
		      } catch (Exception localException) {
		      }
		    return v;
		  }

		  public static float null2Float(Object s) {
		    float v = 0.0F;
		    if (s != null)
		      try {
		        v = Float.parseFloat(s.toString());
		      } catch (Exception localException) {
		      }
		    return v;
		  }

		  public static double null2Double(Object s) {
		    double v = 0.0D;
		    if (s != null)
		      try {
		        v = Double.parseDouble(s.toString());
		      } catch (Exception localException) {
		      }
		    return v;
		  }

		  public static boolean null2Boolean(Object s) {
		    boolean v = false;
		    if (s != null)
		      try {
		        v = Boolean.parseBoolean(s.toString());
		      } catch (Exception localException) {
		      }
		    return v;
		  }

		  public static String null2String(Object s) {
		    return s == null ? "" : s.toString();
		  }

		  public static Long null2Long(Object s) {
		    Long v = Long.valueOf(-1L);
		    if (s != null)
		      try {
		        v = Long.valueOf(Long.parseLong(s.toString()));
		      } catch (Exception localException) {
		      }
		    return v;
		  }

		  public static String getTimeInfo(long time) {
		    int hour = (int)time / 3600000;
		    long balance = time - hour * 1000 * 60 * 60;
		    int minute = (int)balance / 60000;
		    balance -= minute * 1000 * 60;
		    int seconds = (int)balance / 1000;
		    String ret = "";
		    if (hour > 0)
		      ret = ret + hour + "小时";
		    if (minute > 0)
		      ret = ret + minute + "分";
		    else if ((minute <= 0) && (seconds > 0))
		      ret = ret + "零";
		    if (seconds > 0)
		      ret = ret + seconds + "秒";
		    return ret;
		  }

		  public static String getIpAddr(HttpServletRequest request) {
		    String ip = request.getHeader("x-forwarded-for");
		    if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
		      ip = request.getHeader("Proxy-Client-IP");
		    }
		    if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
		      ip = request.getHeader("WL-Proxy-Client-IP");
		    }
		    if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
		      ip = request.getRemoteAddr();
		    }
		    return ip;
		  }

		  public static int indexOf(String s, String sub) {
		    return s.trim().indexOf(sub.trim());
		  }

		  public static Map cal_time_space(Date begin, Date end) {
		    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		    long l = end.getTime() - begin.getTime();
		    long day = l / 86400000L;
		    long hour = l / 3600000L - day * 24L;
		    long min = l / 60000L - day * 24L * 60L - hour * 60L;
		    long second = l / 1000L - day * 24L * 60L * 60L - hour * 60L * 60L - min * 60L;
		    Map map = new HashMap();
		    map.put("day", Long.valueOf(day));
		    map.put("hour", Long.valueOf(hour));
		    map.put("min", Long.valueOf(min));
		    map.put("second", Long.valueOf(second));
		    return map;
		  }

		  public static final String randomString(int length) {
		    char[] numbersAndLetters = "0123456789abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
		      .toCharArray();
		    if (length < 1) {
		      return "";
		    }
		    Random randGen = new Random();
		    char[] randBuffer = new char[length];
		    for (int i = 0; i < randBuffer.length; i++) {
		      randBuffer[i] = numbersAndLetters[randGen.nextInt(71)];
		    }
		    return new String(randBuffer);
		  }

		  public static final String randomInt(int length) {
		    if (length < 1) {
		      return null;
		    }
		    Random randGen = new Random();
		    char[] numbersAndLetters = "0123456789".toCharArray();

		    char[] randBuffer = new char[length];
		    for (int i = 0; i < randBuffer.length; i++) {
		      randBuffer[i] = numbersAndLetters[randGen.nextInt(10)];
		    }
		    return new String(randBuffer);
		  }

		  public static long getDateDistance(String time1, String time2)
		  {
		    long quot = 0L;
		    SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
		    try {
		      Date date1 = ft.parse(time1);
		      Date date2 = ft.parse(time2);
		      quot = date1.getTime() - date2.getTime();
		      quot = quot / 1000L / 60L / 60L / 24L;
		    } catch (ParseException e) {
		      e.printStackTrace();
		    }
		    return quot;
		  }

		  public static double div(Object a, Object b)
		  {
		    double ret = 0.0D;
		    if ((!null2String(a).equals("")) && (!null2String(b).equals(""))) {
		      BigDecimal e = new BigDecimal(null2String(a));
		      BigDecimal f = new BigDecimal(null2String(b));
		      if (null2Double(f) > 0.0D)
		        ret = e.divide(f, 3, 1).doubleValue();
		    }
		    DecimalFormat df = new DecimalFormat("0.00");
		    return Double.valueOf(df.format(ret)).doubleValue();
		  }

		  public static double subtract(Object a, Object b)
		  {
		    double ret = 0.0D;
		    BigDecimal e = new BigDecimal(null2Double(a));
		    BigDecimal f = new BigDecimal(null2Double(b));
		    ret = e.subtract(f).doubleValue();
		    DecimalFormat df = new DecimalFormat("0.00");
		    return Double.valueOf(df.format(ret)).doubleValue();
		  }

		  public static double add(Object a, Object b)
		  {
		    double ret = 0.0D;
		    BigDecimal e = new BigDecimal(null2Double(a));
		    BigDecimal f = new BigDecimal(null2Double(b));
		    ret = e.add(f).doubleValue();
		    DecimalFormat df = new DecimalFormat("0.00");
		    return Double.valueOf(df.format(ret)).doubleValue();
		  }

		  public static double mul(Object a, Object b)
		  {
		    BigDecimal e = new BigDecimal(null2Double(a));
		    BigDecimal f = new BigDecimal(null2Double(b));
		    double ret = e.multiply(f).doubleValue();
		    DecimalFormat df = new DecimalFormat("0.00");
		    return Double.valueOf(df.format(ret)).doubleValue();
		  }

		  public static double formatMoney(Object money) {
		    DecimalFormat df = new DecimalFormat("0.00");
		    return Double.valueOf(df.format(money)).doubleValue();
		  }

		  public static int M2byte(float m) {
		    float a = m * 1024.0F * 1024.0F;
		    return (int)a;
		  }

		  public static boolean convertIntToBoolean(int intValue) {
		    return intValue != 0;
		  }

		  public static String getURL(HttpServletRequest request) {
		    String contextPath = request.getContextPath().equals("/") ? "" : 
		      request.getContextPath();
		    String url = "http://" + request.getServerName();
		    if (null2Int(Integer.valueOf(request.getServerPort())) != 80)
		      url = url + ":" + null2Int(Integer.valueOf(request.getServerPort())) + contextPath;
		    else {
		      url = url + contextPath;
		    }
		    return url;
		  }

		  public static String filterHTML(String content)
		  {
		    Whitelist whiteList = new Whitelist();
		    String s = Jsoup.clean(content, user_content_filter).replaceAll("pre", 
		      "div");
		    return s;
		  }

		  public static String renderHtml(String url, String uid)
		  {
		    String uc_api = URLEncoder.encode(url + "/avatar_upload");
		    String flash = url + 
		      "/resources/flash/camera.swf?inajax=1&appid=1&input=" + 
		      UUID.randomUUID().toString() + "@" + uid + "&ucapi=" + uc_api + 
		      "&avatartype=virtual&uploadSize=1024";
		    StringBuffer s = new StringBuffer(
		      "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0\" width=\"450\" height=\"253\" id=\"mycamera\" align=\"middle\">");
		    s.append("<param name=\"allowScriptAccess\" value=\"always\" />");
		    s.append("<param name=\"scale\" value=\"exactfit\" />");
		    s.append("<param name=\"wmode\" value=\"transparent\" />");
		    s.append("<param name=\"quality\" value=\"high\" />");
		    s.append("<param name=\"bgcolor\" value=\"#ffffff\" />");
		    s.append("<param name=\"movie\" value=\"" + flash + "\" />");
		    s.append("<param name=\"menu\" value=\"false\" />");
		    s
		      .append("<embed src=\"" + 
		      flash + 
		      "\" quality=\"high\" bgcolor=\"#ffffff\" width=\"450\" height=\"253\" name=\"mycamera\" align=\"middle\" allowScriptAccess=\"always\" allowFullScreen=\"false\" scale=\"exactfit\"  wmode=\"transparent\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" />");
		    s.append("</object>");
		    return s.toString();
		  }

		  public static int parseDate(String type, Date date) {
		    Calendar cal = Calendar.getInstance();
		    cal.setTime(date);
		    if (type.equals("y")) {
		      return cal.get(1);
		    }
		    if (type.equals("M")) {
		      return cal.get(2) + 1;
		    }
		    if (type.equals("d")) {
		      return cal.get(5);
		    }
		    if (type.equals("H")) {
		      return cal.get(11);
		    }
		    if (type.equals("m")) {
		      return cal.get(12);
		    }
		    if (type.equals("s")) {
		      return cal.get(13);
		    }
		    return 0;
		  }

		  public static int[] readImgWH(String imgurl)
		  {
		    boolean b = false;
		    try
		    {
		      URL url = new URL(imgurl);

		      BufferedInputStream bis = new BufferedInputStream(
		        url.openStream());

		      byte[] bytes = new byte[100];

		      OutputStream bos = new FileOutputStream(new File(
		        "C:\\thetempimg.gif"));
		      int len;
		      while ((len = bis.read(bytes)) > 0)
		      {
		        bos.write(bytes, 0, len);
		      }
		      bis.close();
		      bos.flush();
		      bos.close();

		      b = true;
		    }
		    catch (Exception e) {
		      b = false;
		    }
		    int[] a = new int[2];
		    if (b)
		    {
		      File file = new File("C:\\thetempimg.gif");
		      BufferedImage bi = null;
		      boolean imgwrong = false;
		      try
		      {
		        bi = ImageIO.read(file);
		        try
		        {
		          int i = bi.getType();
		          imgwrong = true;
		        } catch (Exception e) {
		          imgwrong = false;
		        }
		      } catch (IOException ex) {
		        ex.printStackTrace();
		      }
		      if (imgwrong) {
		        a[0] = bi.getWidth();
		        a[1] = bi.getHeight();
		      } else {
		        a = (int[])null;
		      }

		      file.delete();
		    } else {
		      a = (int[])null;
		    }
		    return a;
		  }

		  public static boolean del_acc(HttpServletRequest request, Accessory acc)
		  {
		    boolean ret = true;
		    boolean ret1 = true;
		    if (acc != null) {
		      String path = request.getRealPath("/") + acc.getPath() + 
		        File.separator + acc.getName();
		      String small_path = request.getRealPath("/") + acc.getPath() + 
		        File.separator + acc.getName() + "_small." + acc.getExt();
		      ret = deleteFile(path);
		      ret1 = deleteFile(small_path);
		    }
		    return (ret) && (ret1);
		  }

		  public static boolean fileExist(String path)
		  {
		    File file = new File(path);
		    return file.exists();
		  }

		  public static int splitLength(String s, String c)
		  {
		    int v = 0;
		    if (!s.trim().equals("")) {
		      v = s.split(c).length;
		    }
		    return v;
		  }

		  public static double fileSize(File file)
		  {
		    if (file == null) {
		      return 0.0D;
		    }
		    if (!file.isDirectory()) {
		      return 0.0D;
		    }
		    double fileSize = 0.0D;
		    File[] files = file.listFiles();
		    for (File f : files) {
		      if (f.isFile()) {
		        fileSize += f.length();
		      } else if (f.isDirectory()) {
		        fileSize += f.length();
		        fileSize += fileSize(f);
		      }
		    }
		    return div(Double.valueOf(fileSize), Integer.valueOf(1024));
		  }

		  public static int fileCount(File file)
		  {
		    if (file == null) {
		      return 0;
		    }
		    if (!file.isDirectory()) {
		      return 1;
		    }
		    int fileCount = 0;
		    File[] files = file.listFiles();
		    for (File f : files) {
		      if (f.isFile()) {
		        fileCount++;
		      } else if (f.isDirectory()) {
		        fileCount++;
		        fileCount += fileCount(file);
		      }
		    }
		    return fileCount;
		  }

		  public static String get_all_url(HttpServletRequest request)
		  {
		    String query_url = request.getRequestURI();
		    if ((request.getQueryString() != null) && 
		      (!request.getQueryString().equals(""))) {
		      query_url = query_url + "?" + request.getQueryString();
		    }
		    return query_url;
		  }

		  public static Color getColor(String color)
		  {
		    if (color.charAt(0) == '#') {
		      color = color.substring(1);
		    }
		    if (color.length() != 6)
		      return null;
		    try
		    {
		      int r = Integer.parseInt(color.substring(0, 2), 16);
		      int g = Integer.parseInt(color.substring(2, 4), 16);
		      int b = Integer.parseInt(color.substring(4), 16);
		      return new Color(r, g, b); } catch (NumberFormatException nfe) {
		    }
		    return null;
		  }

		  public static Set<Integer> randomInt(int a, int length)
		  {
		    Set list = new TreeSet();
		    int size = length;
		    if (length > a) {
		      size = a;
		    }
		    while (list.size() < size) {
		      Random random = new Random();
		      int b = random.nextInt(a);
		      list.add(Integer.valueOf(b));
		    }
		    return list;
		  }

		  public static Double formatDouble(Object obj, int len)
		  {
		    Double ret = Double.valueOf(0.0D);
		    String format = "0.0";
		    for (int i = 1; i < len; i++) {
		      format = format + "0";
		    }
		    DecimalFormat df = new DecimalFormat(format);
		    return Double.valueOf(df.format(obj));
		  }
}
